{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "58aa3d75-5a9b-43d8-8bea-f391319e5710",
   "metadata": {},
   "source": [
    "Chapter 03\n",
    "\n",
    "# 矩阵乘法第四视角\n",
    "《线性代数》 | 鸢尾花书：数学不难"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e280b673-1a06-4913-ba7b-2f39f60cd7e1",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "154068f3-d35a-48f9-a120-e6c5c276c659",
   "metadata": {},
   "source": [
    "## 初始化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "06bbbb2f-a859-41e4-8073-f154fe95ee4e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7ba209c7-e728-44b6-b6ac-a04f59f37e37",
   "metadata": {},
   "source": [
    "## 定义矩阵 A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "99542863-0c1d-4fac-a6ea-1441d2085d97",
   "metadata": {},
   "outputs": [],
   "source": [
    "A = np.array([[1, 2, 3], \n",
    "              [4, 5, 6]])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e0bf4ae-7bf6-4963-b49f-e18db500d223",
   "metadata": {},
   "source": [
    "## 计算矩阵 B，即 A 的转置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "30eff698-6fec-4b5d-ab1c-cd5e02b5e0e8",
   "metadata": {},
   "outputs": [],
   "source": [
    "B = A.T"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e354c300-c5aa-48c1-8624-637c1da961fc",
   "metadata": {},
   "source": [
    "## 计算矩阵乘法 C = A @ B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "c444762e-ef35-4a4f-83ad-a217be49be42",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[14, 32],\n",
       "       [32, 77]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "C = A @ B\n",
    "C"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c29675d-e59a-4874-bf56-b164496d14a4",
   "metadata": {},
   "source": [
    "## 第一行 $c^{(1)}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "4d54dc5d-5b59-4396-ae7f-b9a7e1482d03",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A[[0],:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "5e241bcc-48c4-499c-840d-4263605aea08",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 4],\n",
       "       [2, 5],\n",
       "       [3, 6]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "c5bdb5d4-4085-4542-9c21-13d2375f769f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[14, 32]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c_1_row = A[[0],:] @ B\n",
    "c_1_row"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "09c738ca-e373-4a2d-809e-df872f023229",
   "metadata": {},
   "source": [
    "## 第二行 $c^{(2)}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "308b3a89-255b-427a-9376-2a994e8ad33f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 5, 6]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A[[1],:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "12b5d868-c60f-49eb-8938-f466a2e0dce5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 4],\n",
       "       [2, 5],\n",
       "       [3, 6]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "6f84da08-a6bf-4fda-b29e-0674c5729c92",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[32, 77]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c_2_row = A[[1],:] @ B\n",
    "c_2_row"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5b99c40e-97a6-4514-92a1-2e50efb0f56b",
   "metadata": {},
   "source": [
    "## 上下排列得到C"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "dc1b7a7f-525b-4d22-975d-6e13afa71589",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[14, 32],\n",
       "       [32, 77]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.block([[c_1_row], [c_2_row]])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "72f74467-3c52-44c9-bcdf-0e37cf5d3e10",
   "metadata": {},
   "source": [
    "## 计算B @ A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "b8939b70-2dda-4933-8111-8d57a92495da",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[17, 22, 27],\n",
       "       [22, 29, 36],\n",
       "       [27, 36, 45]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "D = B @ A\n",
    "D"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "239a15a6-1422-47b7-ba4a-8f5d9c2c6cc8",
   "metadata": {},
   "source": [
    "## 第一行 $d^{(1)}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "b170038d-bab9-46bb-9cb9-3ffe8fabf48c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 4]])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B[[0],:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "770211de-9caa-4abe-816d-85e5ee630736",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "4437cb27-8643-4285-9a35-5a35167c0e77",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[17, 22, 27]])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d_1_row = B[[0],:] @ A\n",
    "d_1_row"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e25f6b6-75e2-4d52-8dea-ac0654f0c196",
   "metadata": {},
   "source": [
    "## 第二行 $d^{(2)}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "2bde3904-00ab-4321-b06f-e5f631140206",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 5]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B[[1],:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "eec666f2-5f2f-4433-8490-c9addcda4c95",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "e17bf480-d7f0-4974-9a05-891c53acb884",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[22, 29, 36]])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d_2_row = B[[1],:] @ A\n",
    "d_2_row"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a300737-5422-48d4-800b-1b68bf18907e",
   "metadata": {},
   "source": [
    "## 第三行 $d^{(2)}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "6687cd61-ad21-4891-897a-cdbc121222aa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 6]])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B[[2],:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "1fd7805a-1263-4764-981e-bd6c50679ee1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "7a153751-ea6b-4559-8ca7-12b40395529a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[27, 36, 45]])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d_3_row = B[[2],:] @ A\n",
    "d_3_row"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bff02253-44ef-46cd-ba41-b0ca762357f6",
   "metadata": {},
   "source": [
    "## 上下排列得到D"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "6af4530c-883e-4e89-a7d2-741d76943516",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[17, 22, 27],\n",
       "       [22, 29, 36],\n",
       "       [22, 29, 36]])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.block([[d_1_row], [d_2_row], [d_2_row]])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0bdec93f-0880-46f3-b46f-2c0ee3fdbbdb",
   "metadata": {},
   "source": [
    "作者\t**生姜DrGinger**  \n",
    "脚本\t**生姜DrGinger**  \n",
    "视频\t**崔崔CuiCui**  \n",
    "开源资源\t[**GitHub**](https://github.com/Visualize-ML)  \n",
    "平台\t[**油管**](https://www.youtube.com/@DrGinger_Jiang)\t\t\n",
    "\t\t[**iris小课堂**](https://space.bilibili.com/3546865719052873)\t\t\n",
    "\t\t[**生姜DrGinger**](https://space.bilibili.com/513194466)  "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
